package org.sapidoc.idoc.generatorclass;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.sapidoc.idoc.generatorclass.parser.ClassGenerator;

public class Generator {

	private static enum ArgumentEnum{
		PACKAGE("p","package of object generation"),
		IDOC_DEF_FILE("f","Idoc file definition"),
		TARGET_DIR("d","Root folder of Object generation");	
		private ArgumentEnum(String code,String description) {
			this.code = code;
			this.description = description;
		}
		private String code;
		private String description;
		
		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}
		
		public static ArgumentEnum getByCode(String code){
			for(ArgumentEnum argumentEnum : values()){
				if(argumentEnum.getCode().equals(code)){
					return argumentEnum;
				}
			}
			return null;
		}

	}
	
	private static class Argument<T>{
		private String option;
		private String value;
		public String getOption() {
			return option;
		}
		public void setOption(String option) {
			this.option = option;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
		
		@Override
		public int hashCode() {
			if(option == null){
				return super.hashCode();
			}
			else{
				return option.hashCode();
			}
		}
		
		@Override
		protected Argument<T> clone(){
			Argument<T> argument = new Argument<T>();
			argument.setOption(getOption());
			argument.setValue(getValue());
			return argument;
		}
	}
	
	private static Map<String,Argument<String>> constructArgument(String...args) throws IllegalArgumentException{
		Map<String,Argument<String>> map = new HashMap<String, Argument<String>>();
		if(args.length <6 ){
			throw new IllegalArgumentException("Problem when generating class, you must provide all option");
		}
		Argument<String> argument = null;
		for(int i=0;i<args.length;i++){
			String arg = args[i];
			if(arg.startsWith("-")){
				arg = arg.substring(1, arg.length());
			}
			ArgumentEnum argumentEnum = ArgumentEnum.getByCode(arg);
			if(argumentEnum != null){
				argument = new Argument<String>();
				argument.setOption(argumentEnum.getCode());
				continue;
			}
			if(argument != null && argumentEnum == null){
				argument.setValue(arg);
				map.put(argument.getOption(), argument.clone());
			}
			argument = null;
		}
		return map;
	}
	
	private static void listParam(){
		System.out.println("OPTION LIST : ");
		for(ArgumentEnum argumentEnum  : ArgumentEnum.values()){
			System.out.println("\t-"+argumentEnum.code+" : "+argumentEnum.description);
		}
	}
	
	public static void main(String[] args) throws IOException {
		ClassGenerator gen = new ClassGenerator();
		if(args.length == 0){
			listParam();
			return;
		}
		Map<String,Argument<String>> map = constructArgument(args);
//		gen.useAnnotation();
		gen.setPackageName(map.get(ArgumentEnum.PACKAGE.getCode()).getValue());//"de.albatross.idoc.generate.contractaccount");
		gen.setTargetFile(map.get(ArgumentEnum.IDOC_DEF_FILE.getCode()).getValue());//"D:/workspace/Albatros_svn/Spec/document_Spec/Contract Accounts/PARSER ZUTACCOUNT_CREATEFROMDATA01.txt");
		gen.setTargetDir(map.get(ArgumentEnum.TARGET_DIR.getCode()).getValue());//"D:/temp/test");
		gen.init();
	}
}
